#include "settings.h"

Settings::Settings()
{
	Init();
}

Settings::~Settings()
{

}

void Settings::Init()
{
	_version = 1;

	_soundEnabled = true;
	_musicEnabled = true;
	_advancedControls = false;
	_networkEnabled = false;
		
	//Network Settings
	_portiiIp = " 209.190.24.9";
	_portiiHostName = "www.portii.com";
	_portiiNetworkPath = "/current_release/";
	_levelNetworkPath  = "/levels/";
	
	_userName = "";
}

void Settings::ChangeSettings()
{
   //Allow User to change the settings via a Settings GUI
   
   //TODO Add Settings GUI 
}

void Settings::Load()
{

  FILE *fp;
  mxml_node_t *xml;
  mxml_node_t *node;
  
  fp = fopen("/apps/portii/settings.xml", "r");
  
  if(fp != NULL)
  {
	  xml = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
	  fclose(fp);
			
	  if(xml != NULL)
	  {
		  node = mxmlFindElement(xml,xml, "sound", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  if(atoi(mxmlElementGetAttr(node,"value")))
				_soundEnabled = true;
			  else
				_soundEnabled = false;
		  }
		  
		  node = mxmlFindElement(xml,xml, "music", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  if(atoi(mxmlElementGetAttr(node,"value")))
				_musicEnabled = true;
			  else
				_musicEnabled = false;
		  }
		  
		  node = mxmlFindElement(xml,xml, "network", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  if(atoi(mxmlElementGetAttr(node,"value")))
				_networkEnabled = true;
			  else
				_networkEnabled = false;
		  }
		  
		  node = mxmlFindElement(xml,xml, "advancedControls", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  if(atoi(mxmlElementGetAttr(node,"value")))
				_advancedControls = true;
			  else
				_advancedControls = false;
		  }
		  
		  node = mxmlFindElement(xml,xml, "username", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  _userName = std::string(mxmlElementGetAttr(node,"value"));
		  }
		  
		  node = mxmlFindElement(xml,xml, "network", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  _portiiIp = std::string(mxmlElementGetAttr(node,"ip"));
			  _portiiHostName = std::string(mxmlElementGetAttr(node,"hostname"));
			  _portiiNetworkPath = std::string(mxmlElementGetAttr(node,"portiiNetworkPath"));
			  _levelNetworkPath = std::string(mxmlElementGetAttr(node,"levelNetworkPath"));
		  }
		  
		  node = mxmlFindElement(xml,xml, "version", NULL, NULL, MXML_DESCEND); 
		  if(node != NULL)
		  {
			  _version = atoi(mxmlElementGetAttr(node,"value"));
		  }
		  
	  }
   }
}

void Settings::Save()
{

    mxml_node_t *xml;
	mxml_node_t *tree;
	
    char buffer[50];
	
	xml = mxmlNewXML("1.0");
		
	tree = mxmlNewElement(xml, "sound");
	sprintf(buffer, "%d", (int)_soundEnabled);
	mxmlElementSetAttr(tree, "value", buffer);
  
 
	tree = mxmlNewElement(xml, "music");
	sprintf(buffer, "%d", (int)_musicEnabled);
	mxmlElementSetAttr(tree, "value", buffer);
	
	tree = mxmlNewElement(xml, "network");
	sprintf(buffer, "%d", (int)_networkEnabled);
	mxmlElementSetAttr(tree, "value", buffer);
	
	tree = mxmlNewElement(xml, "advancedControls");
	sprintf(buffer, "%d", (int)_advancedControls);
	mxmlElementSetAttr(tree, "value", buffer);
  
	tree = mxmlNewElement(xml, "username");
	mxmlElementSetAttr(tree, "value", _userName.c_str());
	
	tree = mxmlNewElement(xml, "network");
	mxmlElementSetAttr(tree, "ip", _portiiIp.c_str());
	mxmlElementSetAttr(tree, "hostname", _portiiHostName.c_str());
	mxmlElementSetAttr(tree, "portiiNetworkPath", _portiiNetworkPath.c_str());
	mxmlElementSetAttr(tree, "levelNetworkPath", _levelNetworkPath.c_str());
	
	tree = mxmlNewElement(xml, "version");
	sprintf(buffer, "%d", _version);
	mxmlElementSetAttr(tree, "value", buffer);
	
	FILE *fp;

	fp = fopen("/apps/portii/settings.xml", "w");
			
	if(fp != NULL)
	{
		mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
	
		fclose(fp);

	}
}
